package richbar.com.richbar.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.base.BaseFragment;
import com.common.util.LogUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import richbar.com.richbar.MainActivity;
import richbar.com.richbar.R;
import richbar.com.richbar.adapter.shopping.ShoppingAdapter;
import richbar.com.richbar.adapter.shopping.ShoppingAdapters;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.ShopCartListBean;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

public class ShoppingCartFragments extends BaseFragment {
    private final static String ACTION = "REFRESH";
    private ShoppingCartFragment.MyReceiver myReceiver;
    private ShoppingAdapters adapter;
    ExpandableListView expandableListView;
    LinearLayout ly_nogods;
    TextView tv_go;
    TextView tv_edit;
    TextView tv_finish;
    TextView tv_delete;
    TextView total_money;
    TextView buy;
    List<ShopCartListBean.DataBean.ListBean> groups = new ArrayList<>();
    Map<Integer, Boolean> groupSelect = new HashMap<>();
    List<List<ShopCartListBean.DataBean.ListBean.GoodsBean>> childs = new ArrayList<>();
    List<Map<Integer, Boolean>> childSelect = new ArrayList<>();
    List<Map<Integer, Boolean>> childDelSelect = new ArrayList<>();
    CheckBox selectall;
    CheckBox selectdelall;

    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shopcart, container, false);
        expandableListView = view.findViewById(R.id.ex_listView);
        ly_nogods = view.findViewById(R.id.ly_nogods);
        tv_go = view.findViewById(R.id.tv_go);
        tv_edit = view.findViewById(R.id.tv_edit);
        tv_finish = view.findViewById(R.id.tv_finish);
        tv_delete = view.findViewById(R.id.tv_delete);
        total_money = view.findViewById(R.id.total_money);
        selectall = view.findViewById(R.id.selectall);
        selectdelall = view.findViewById(R.id.selectdelall);
        buy = view.findViewById(R.id.buy);
        tv_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toFragment(1);
            }
        });
        return view;
    }

    @Override
    protected void initData() {
        getData();
        adapter = new ShoppingAdapters(ShoppingCartFragments.this, getActivity(), groups, childs, groupSelect, childSelect, childDelSelect, selectall, selectdelall, total_money, tv_finish, tv_delete, tv_edit, buy);
        expandableListView.setAdapter(adapter);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

    }

    private void getData() {
        NetWorkUtil.getCarList(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final ShopCartListBean shopCartListBean = MyGson.getInstance().fromJson(data, ShopCartListBean.class);
                if (shopCartListBean.getData().getList().size() > 0) {
                    for (int i = 0; i < shopCartListBean.getData().getList().size(); i++) {
                        groups.add(shopCartListBean.getData().getList().get(i));
                        if (shopCartListBean.getData().getList().get(i).getSelect().equals("1"))
                            groupSelect.put(i, true);
                        childs.add(shopCartListBean.getData().getList().get(i).getGoods());

                    }

                    if (childs.size() > 0) {
                        for (int i = 0; i < childs.size(); i++) {
                            HashMap<Integer, Boolean> map = new HashMap<>();
                            HashMap<Integer, Boolean> mapdel = new HashMap<>();
                            for (int j = 0; j < childs.get(i).size(); j++) {
                                if (childs.get(i).get(j).getGoods_select().equals("1")) {
                                    map.put(j, true);
                                }
                            }
                            childSelect.add(map);
                            childDelSelect.add(mapdel);
                        }
                    }
                    if (groups.size() > 0) {
                        for (int i = 0; i < groups.size(); i++) {
                            expandableListView.expandGroup(i);
                        }
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    expandableListView.setVisibility(View.GONE);
                    ly_nogods.setVisibility(View.VISIBLE);
                }
                total_money.setText("合计：" + shopCartListBean.getData().getTotalPrice() + "元");
                if (shopCartListBean.getData().getSelect().equals("1"))
                    selectall.setChecked(true);
                else
                    selectall.setChecked(false);


            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(getActivity(), msg);
            }
        });

    }

    private void toFragment(final int i) {
        final MainActivity mainActivity = (MainActivity) getActivity();
        mainActivity.setFragment2Fragment(new MainActivity.Fragment2Fragment() {
            @Override
            public void gotoFragment(ViewPager viewPager) {
                viewPager.setCurrentItem(i);
            }
        }, i);
        mainActivity.forSkip();
    }


    /**
     * 购物车删除后更新的接口
     */
    public void DelShopCartResh() {
        groups.clear();
        groupSelect.clear();
        childs.clear();
        childSelect.clear();
        childDelSelect.clear();
        getData();
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != myReceiver) {
            getActivity().unregisterReceiver(myReceiver);
            myReceiver = null;
        }
    }
}
